package com.ds.common.route.agent;

import com.ds.common.nacos.ConfigModel;
import lombok.Getter;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class AgentList implements ConfigModel {
    @Getter
    Map<Integer, Agent> agentMap;
    @Getter
    Map<String, Agent> agentMapByDomain;

    List<Agent> agentList;

    public void setAgentList(List<Agent> agentList) {
        this.agentList = agentList;
        if (agentList != null) {
            agentMap = agentList.stream().collect(Collectors.toMap(Agent::getAgentId, Function.identity()));
            agentMapByDomain = agentList.stream().collect(Collectors.toMap(Agent::getAgentBaseDomain, Function.identity()));
        }
    }

    @Override
    public boolean isValid() {
        return agentList != null && !agentList.isEmpty() && agentList.stream().allMatch(Agent::isValid);
    }

}
